package com.pr.makfilm.dao.impl;

import com.pr.makfilm.dao.IDistrictDao;
import com.pr.makfilm.domain.District;
import com.pr.makfilm.domain.ObjectStateEnum;
import com.pr.makfilm.domain.User;
import com.pr.makfilm.domain.filter.AbstractFilter;
import com.pr.makfilm.domain.filter.DistrictFilter;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * User: pawel.radomski
 * Date: 07.12.13
 * Time: 12:49
 */
@Repository(DistrictDaoImpl.BEAN_NAME)
public class DistrictDaoImpl extends AbstractDaoImpl<District> implements IDistrictDao
{
   public static final String BEAN_NAME = "districtDao";

   @Override
   public Criteria createCriteriaFromSearchParams(AbstractFilter abstractFilter)
   {
      Criteria criteria = createCriteria();
      DistrictFilter filter = (DistrictFilter) abstractFilter;
      User user = filter.getUser();
      if (user != null)
      {
         criteria.add(Restrictions.eq(District.FIELD_USER, user));
      }
      String name = filter.getName();
      if (name != null)
      {
         criteria.add(Restrictions.ilike(District.FIELD_NAME, name, MatchMode.ANYWHERE));
      }
      criteria.add(Restrictions.eq(District.FIELD_OBJECT_STATE, ObjectStateEnum.ACTIVE));
      return criteria;
   }

   @Override
   public List<District> findByUser(User user)
   {
      Criteria criteria = createCriteria();
      criteria.add(Restrictions.eq(District.FIELD_OBJECT_STATE, ObjectStateEnum.ACTIVE));
      criteria.add(Restrictions.eq(District.FIELD_USER, user));
      return criteria.list();
   }
}
